package com.sims.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sims.bean.EfileResult;
import com.sims.bean.EfileVolunteer;
import com.sims.bean.ElectricalFile;
import com.sims.bean.Student;
import com.sims.dao.IEfileResultDAO;
import com.sims.dao.IEfileVolunteerDAO;
import com.sims.dao.IElectricalFileDAO;
import com.sims.dao.IStudentDAO;
import com.sims.util.Constant;
import com.sims.util.HttpSessionUtil;

public class ElectricalFileService {
	@Autowired
	private IStudentDAO studentDAO;
	@Autowired
	private IElectricalFileDAO electricalFileDAO;
	@Autowired
	private IEfileResultDAO efileResultDAO;
	@Autowired
	private IEfileVolunteerDAO efileVolunteerDAO;

	public Map<String, String> findElectricalFile(Student student,ElectricalFile electricalFile,List efileResultList, List efileVolunteerList) {
		
		Map<String, String> result = new HashMap<String, String>();
		
		Student sInDB = studentDAO.findById(student.getSid());
		
		ElectricalFile eFileInDB = electricalFileDAO.findById(student.getSid());
		List<EfileResult> efileResultListInDB = efileResultDAO.findBySid(student.getSid());
		List<EfileVolunteer> efileVolunteerListInDB = efileVolunteerDAO.findBySid(student.getSid());
		
		if (sInDB == null) {
			result.put(Constant.ERROR, "Student Not Exists");
			return result;
		}
		
		if (eFileInDB == null) {
			result.put(Constant.ERROR, "E file Not Exists");
			return result;
		}
		
		if (efileResultListInDB == null) {
			result.put(Constant.ERROR, "E file result Not Exists");
			return result;
		}
		
		if (efileVolunteerListInDB == null) {
			result.put(Constant.ERROR, "E file volunteer Not Exists");
			return result;
		}

		BeanUtils.copyProperties(sInDB,student);
		BeanUtils.copyProperties(eFileInDB, electricalFile);
		for (EfileResult temp : efileResultListInDB) {
			efileResultList.add(temp);
		}
		for (EfileVolunteer temp : efileVolunteerListInDB) {
			efileVolunteerList.add(temp);
		}

		result.put(Constant.SUCCESS, null);
		return result;
	}


	public Map<String, String> modifyElectricalFile(Student student,
			ElectricalFile electricalFile,EfileResult efileResult, EfileVolunteer efileVolunteer) {
		Map<String, String> result = new HashMap<String, String>();

		Student s = studentDAO.findBySno(student.getSno());
		if (s == null) {
			result.put(Constant.ERROR, "Student Not Exists");
			return result;
		}
		
		ElectricalFile efileInDB = electricalFileDAO.findById(student.getSid());
		EfileResult erInDB = (EfileResult) efileResultDAO.findBySid(s.getSid()).get(0);
		EfileVolunteer evInDB = (EfileVolunteer) efileVolunteerDAO.findBySid(s.getSid());
		
		BeanUtils.copyProperties(electricalFile, efileInDB, new String[]{ "sid" });
		BeanUtils.copyProperties(efileResult, erInDB , new String[]{ "erid" });
		BeanUtils.copyProperties(efileVolunteer, evInDB , new String[]{ "evid" });

		electricalFileDAO.attachDirty(electricalFile);
		efileResultDAO.attachDirty(erInDB);
		efileVolunteerDAO.attachDirty(evInDB);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
}
